package cn.iocoder.yudao.module.api.trade.order.vo;

import cn.iocoder.yudao.framework.common.validation.InEnum;
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.SliderDesensitize;
import cn.iocoder.yudao.framework.validation.InEnumValue;
import cn.iocoder.yudao.module.api.dine.enums.DineStatusEnum;
import cn.iocoder.yudao.module.api.promotion.groupon.dto.GrouponPackageRespDTO;
import cn.iocoder.yudao.module.api.shop.info.dto.ShopInfoDTO;
import cn.iocoder.yudao.module.api.trade.delivery.enums.DeliveryTypeEnum;
import cn.iocoder.yudao.module.api.trade.order.dto.TradeOrderItemRespDTO;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 订单项信息 VO
 *
 * @author 山野羡民
 */
@Data
public class AppTradeOrderPageVO {
    @Schema(description = "订单编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1024")
    private Long id;

    @Schema(description = "店铺编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1024")
    private Long shopId;

    @Schema(description = "店铺信息")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private ShopInfoDTO shop;

    @Schema(description = "交易单号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1146347329394184195")
    private String no;

    @Schema(description = "订单类型：0-普通、1-秒杀、2-砍价、3-拼团、4-积分兑换、5-联盟豆兑换、6-团购、10-外送、20-美食、30-酒店、40-娱乐、50-家政、999-赠品", example = "0")
    private Integer type;

    @Schema(description = "订单状态：0-待付款、10-已付款（待发货、待配送、待上门）、20-进行中（运送中、配送中、服务中、待核销）、30-已完成（已收货、已服务、已取餐、已核销）、40-已关闭", requiredMode = Schema.RequiredMode.REQUIRED, example = "0")
    private Integer status;

    @Schema(description = "订单状态名称", example = "待付款")
    private String statusName;

    @Schema(description = "是否已支付", requiredMode = Schema.RequiredMode.REQUIRED, example = "true")
    private Boolean payStatus;

    @Schema(description = "购买的产品数量", requiredMode = Schema.RequiredMode.REQUIRED, example = "10")
    private Integer productCount;

    @Schema(description = "是否评价", requiredMode = Schema.RequiredMode.REQUIRED, example = "true")
    private Boolean commentStatus;

    @Schema(description = "售后状态：10-申请中、20-卖家通过、30-待卖家收货、40-等待平台退款、50-完成、61-买家取消售后、62-卖家拒绝、63-卖家拒绝收货", requiredMode = Schema.RequiredMode.REQUIRED, example = "10")
    private Integer afterSaleStatus;

    @Schema(description = "退款状态：0-未退款、10-部分退款、20-全部退款", requiredMode = Schema.RequiredMode.REQUIRED, example = "0")
    private Integer refundStatus;

    @Schema(description = "创建时间", requiredMode = Schema.RequiredMode.REQUIRED)
    private LocalDateTime createTime;

    // ========== 价格 + 支付基本信息 ==========

    @Schema(description = "支付订单编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "PAY202412011838591")
    private String payNo;

    @Schema(description = "应付金额，单位：分", requiredMode = Schema.RequiredMode.REQUIRED, example = "1000")
    private Integer payPrice;

    @Schema(description = "积分抵扣的金额", requiredMode = Schema.RequiredMode.REQUIRED, example = "100")
    private Integer pointPrice;

    @Schema(description = "使用的积分", requiredMode = Schema.RequiredMode.REQUIRED, example = "0")
    private Integer deductPoint;

    @Schema(description = "联盟豆抵扣的金额", requiredMode = Schema.RequiredMode.REQUIRED, example = "100")
    private Integer cryptocurrencyPrice;

    @Schema(description = "使用的联盟豆", requiredMode = Schema.RequiredMode.REQUIRED, example = "0")
    private Integer deductCryptocurrency;

    // ========== 收件 + 物流基本信息 ==========

    @Schema(description = "配送类型/服务方式：0-无、1-快递发货、2-到店自提、3-同城配送、4-现场核销、5-上门服务", example = "0")
    @InEnum(DeliveryTypeEnum.class)
    private Integer deliveryType;

    /**
     * 订单项数组
     */
    private List<TradeOrderItemRespDTO> items;

    public List<TradeOrderItemRespDTO> getItems() {
        if (items == null) {
            items = new ArrayList<>();
        }
        return items;
    }

    @Schema(description = "拼团记录编号，拼团订单才有值", example = "100")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Long combinationRecordId;

    @Schema(description = "拼团状态：0-进行中、1-拼团成功、2-拼团失败。拼团记录编号不为空时有效", example = "0")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Integer combinationStatus;

    @Schema(description = "秒杀活动编号，秒杀订单才有值", example = "100")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Long seckillActivityId;

    @Schema(description = "砍价活动编号，砍价订单才有值", example = "100")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Long bargainActivityId;

    @Schema(description = "砍价记录编号，砍价订单才有值", example = "100")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Long bargainRecordId;

    @Schema(description = "积分兑换活动的编号，积分兑换订单才有值", example = "100")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Long pointActivityId;

    @Schema(description = "点餐状态：0-无、10-待接单、20-备餐中、21-已拒单、22-已派单、30-已出餐、40-配送中、50-已送达", example = "1")
    @InEnumValue(DineStatusEnum.class)
    private Integer dineStatus;
    private String dineStatusName;

    @SuppressWarnings("unused")
    public String getDineStatusName() {
        if (dineStatusName == null) {
            return DineStatusEnum.getByValue(getDineStatus()).getName();
        }
        return dineStatusName;
    }

    @Schema(description = "接单时间，点餐下单时提供")
    private LocalDateTime acceptedTime;

    @Schema(description = "派单时间，点餐下单时提供")
    private LocalDateTime assignedTime;

    @Schema(description = "出餐时间，点餐下单时提供")
    private LocalDateTime packedTime;

    @Schema(description = "配送人员编号，外卖派单时提供", example = "100")
    private Long riderStaffId;

    @Schema(description = "餐桌编号，点餐订单才有值", example = "100")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Long dineDeskId;

    @Schema(description = "用餐人数，点餐订单才有值", example = "100")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Long dinePeoples;

    @Schema(description = "预定日期，预约下单才有值")
    private LocalDate reserveDate;

    @Schema(description = "预定时间段，如12:00-12:30，预约下单才有值")
    private String reservePeriod;

    @Schema(description = "团购套餐编号，团购订单才有值")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Long grouponPackageId;

    @Schema(description = "团购套餐，团购订单才有值")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private GrouponPackageRespDTO grouponPackage;

    @Schema(description = "核销码/取件码/取餐号，到店核销的配送类型/服务方式才有值", example = "40964096")
    @SliderDesensitize(prefixKeep = 1, suffixKeep = 1, disable = "!@mm.isMerchant()")
    private String verifyCode;// 为安全考虑，核销码只能用户提供，不能让管理员、商家等角色看到

    @Schema(description = "核销码/取件码/取餐号，BASE64格式的二维码图片数据")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String qrcode;

}
